package com.example.lists;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hej
 * @date 2024/10/11 15:15
 */
public class ListContains {
    public static void main(String[] args) {
        //判断集合是否有交集
        List<String> firstList = Arrays.asList("11", "22", "33");
        List<String> secondList = Arrays.asList("44", "55");

        //全限定名为java.util.Collections，在俩个集合没有交集的时候会返回true，否则返回false。
        if (CollectionUtils.containsAny(firstList, secondList)) {
            System.out.println("CollectionUtils.containsAny方法：firstList、secondList有交集");
        } else if (!CollectionUtils.containsAny(firstList, secondList)) {
            System.out.println("CollectionUtils.containsAny方法：firstList、secondList没有交集");
        }

        //全限定名为：cn.hutool.core.collection.CollectionUtil，两个集合有交集会返回true，否则会返回false，跟Collections.disjoint相反
        if (CollectionUtils.containsAny(firstList, secondList)) {
            System.out.println("CollectionUtils.containsAny方法：firstList、secondList有交集");
        } else if (!CollectionUtils.containsAny(firstList, secondList)) {
            System.out.println("CollectionUtils.containsAny方法：firstList、secondList没有交集");
        }

        //流
        List<String> resultList = firstList.stream().filter(secondList::contains).collect(Collectors.toList());
        if (resultList != null && resultList.size() > 0) {
            System.out.println("firstList、secondList有交集");
        } else {
            System.out.println("firstList、secondList没有交集");
        }
    }

}
